package com.example.myapplication.drag;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.myapplication.KR;
import com.example.myapplication.NineFunSdkGiftbagActivity;
import com.example.myapplication.ResourceUtil;
import java.util.Timer;
import java.util.TimerTask;

/***
 *
 *类说明 ：悬浮窗小球
 *
 *
 */

public class CSFloatView {
	
	private Context mContext;
	private View floatView;
	private WindowManager mManager;
	private WindowManager.LayoutParams params;
	private LinearLayout leftLayout,ll_csfloat_hint,ll_csfloat_right_hint;
	private ImageView imageview;
	private int mScreenWidth,mScreenHeigh;
	private boolean isDowned=false;
	private int mViewWidth,mViewheight;
	private static boolean existView=false;
	private TextView mTxtAccount , mTxtService , mTxtGame , mTxtHide ,mfloatHide;
	public static final int ACCOUNT_MANAGER = 0 ;
	public static final int GAME_SERVICE = 1 ;
	public static final int MORE_GAME = 2 ;
	public static final int HIDE_ICON = 3 ;
	public static final int FLOAT_MENU_LAYOUT = 4 ;
	private boolean isRegister=false;
	private boolean timerIsRun=false;
	private Timer timer;
	private boolean isLeft=true;






	
	public CSFloatView(Context context, boolean register) {
		mContext=context;
		isRegister=register;
		initView();
	}

	public void setIsRegister(boolean register){
		isRegister=register;
		if(isRegister){
			setTimer();
		}
	}

	private void initView(){
		if(existView){
			return;
		}
		mScreenWidth = mContext.getResources().getDisplayMetrics().widthPixels;
		mScreenHeigh=mContext.getResources().getDisplayMetrics().heightPixels;
		mManager = (WindowManager) mContext
				.getSystemService(Context.WINDOW_SERVICE);
		params = new WindowManager.LayoutParams(
				WindowManager.LayoutParams.WRAP_CONTENT,
				WindowManager.LayoutParams.WRAP_CONTENT,
				WindowManager.LayoutParams.LAST_APPLICATION_WINDOW,
				WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
						| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
						| WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,// FLAG_LAYOUT_NO_LIMITS使x轴可以超出手机屏幕外
				PixelFormat.TRANSLUCENT);
		//这里设置需要显示位置 top left  right  buttom 其中 左上 左下 右上 右下 4个位置
		params.gravity= Gravity.TOP | Gravity.LEFT | Gravity.CENTER_VERTICAL;
		params.width = WindowManager.LayoutParams.WRAP_CONTENT;
		//需要根据横竖屏处理
		//  这里我们可以设置左边是靠上边或者下边多少距离
		params.x=0;
//		params.y=mScreenHeigh/2-100;
		params.y = 100;
		findViewByid(1);
		mHandler.postDelayed(run, 2000);		
	}
	
	
	private int initialX;
	private int initialY;
	private float initialTouchX;
	private float initialTouchY;
	private int delay;
	class ImageviewOnTouch implements OnTouchListener {

		@Override
		public boolean onTouch(View view, MotionEvent event) {
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				initialX = params.x;
				initialY = params.y;
				initialTouchX = event.getRawX();
				initialTouchY = event.getRawY();
				mHandler.removeCallbacks(run);
				break;

			case MotionEvent.ACTION_UP:
				
				if(!isDowned){
					
					if(Math.abs((event.getRawX() - initialTouchX))>10){
						
						Log.e("tag", "移动："+(event.getRawX() - initialTouchX));
						if(initialX < 0){
							params.x = 0 ;
							mManager.updateViewLayout(floatView, params);
						}
						if(initialX > mScreenWidth - mViewWidth ){
							params.x = mScreenWidth - mViewWidth;
							mManager.updateViewLayout(floatView, params);
						}
						if(params.y<0){
							params.y=0;
						}if(params.y+mViewheight>=mScreenHeigh){
							params.y=mScreenHeigh-mViewheight;
						}
						mHandler.postDelayed(run, 2000);
						
					}else{

						params.width=mViewWidth*5;
						if(params.x>mScreenWidth/2){
							isLeft=false;
							findViewByid(0);
						}else{
							isLeft=true;
							if(params.x<0){
								params.x=0;
							}
							findViewByid(1);
						}
						delay=params.x;
						isDowned = false;
						//弹出菜单界面
						mContext.startActivity(new Intent(mContext, NineFunSdkGiftbagActivity.class));
						mManager.updateViewLayout(floatView, params);
						mHandler.postDelayed(run, 2000);

					}

				}else{
					mHandler.removeCallbacks(run);
					isDowned=false;
					if(!isLeft){
						leftLayout.setVisibility(View.GONE);
					}else{
						leftLayout.setVisibility(View.GONE);
						params.width=mViewWidth;
					}
					if(delay>=(mScreenWidth-mViewWidth/2)){
						params.x=mScreenWidth-mViewWidth;
					}else{
						params.x=delay;
					}

					mManager.updateViewLayout(floatView, params);
					mHandler.postDelayed(run, 2000);
				}
				

				break;
			case MotionEvent.ACTION_MOVE:
				if(!isDowned){
					params.x = initialX + (int) (event.getRawX() - initialTouchX);
					params.y = initialY + (int) (event.getRawY() - initialTouchY);
					mManager.updateViewLayout(floatView, params);
				}
				break;
			}
			return true;
		}
		
	}

	private void updateView(){

		if(params.x>(mScreenWidth/2)){
			params.x=(mScreenWidth-mViewWidth/3);
			params.alpha = 0.5f;
			mManager.updateViewLayout(floatView, params);
		}else{
			params.x=(mViewWidth/3)-mViewWidth;
			params.alpha = 0.5f;
			mManager.updateViewLayout(floatView, params);
		}
		params.alpha = 1f;
	}
	
	private Handler mHandler=new Handler();
	Runnable run=new Runnable() {
		
		@Override
		public void run() {
			updateView();
		}
	};
	
	@SuppressLint("Range")
	private void findViewByid(int type){
		if(floatView!=null){
			mManager.removeView(floatView);
		}
		if(type==1){
			floatView= LayoutInflater.from(mContext).inflate(ResourceUtil.getLayoutId(mContext, KR.layout.cs_floatview_left), null, false);
			leftLayout=(LinearLayout)floatView.findViewById(ResourceUtil.getId(mContext, KR.id.ll_floatview_left_extends));
			ll_csfloat_hint=(LinearLayout)floatView.findViewById(ResourceUtil.getId(mContext, KR.id.ll_csfloat_hint));
			ll_csfloat_right_hint=(LinearLayout)floatView.findViewById(ResourceUtil.getId(mContext, KR.id.ll_csfloat_right_hint));
		}else{
			floatView= LayoutInflater.from(mContext).inflate(ResourceUtil.getLayoutId(mContext, KR.layout.cs_floatview_right), null, false);
			leftLayout=(LinearLayout)floatView.findViewById(ResourceUtil.getId(mContext, KR.id.ll_floatview_right_extends));
			ll_csfloat_hint=(LinearLayout)floatView.findViewById(ResourceUtil.getId(mContext, KR.id.ll_csfloat_right_hint));
		}
		
		floatView.setAlpha(100);
		imageview=(ImageView)floatView.findViewById(ResourceUtil.getId(mContext, KR.id.iv_floatview_left));
		leftLayout.setVisibility(View.GONE);
		ll_csfloat_hint.setVisibility(View.GONE);
		imageview.setOnTouchListener(new ImageviewOnTouch());
		imageview.getViewTreeObserver().addOnGlobalLayoutListener(
				new OnGlobalLayoutListener() {
					@Override
					public void onGlobalLayout() {
						mViewWidth = imageview.getWidth();
						mViewheight=imageview.getHeight();
						imageview.getViewTreeObserver()
								.removeGlobalOnLayoutListener(this);
					}
				});
		mManager.addView(floatView, params);
		setOnClickListener();
		existView=true;
		if(isRegister){
			setTimer();
		}
	}
	
	private void setOnClickListener(){
		mTxtAccount  = (TextView) floatView.findViewById(ResourceUtil.getId(mContext, KR.id.txt_floatmenu_account));
		mTxtService  = (TextView) floatView.findViewById(ResourceUtil.getId(mContext, KR.id.txt_floatmenu_service));
		mTxtGame  = (TextView) floatView.findViewById(ResourceUtil.getId(mContext, KR.id.txt_floatmenu_game));
		mTxtHide  = (TextView) floatView.findViewById(ResourceUtil.getId(mContext, KR.id.txt_floatmenu_hide));
		TextViewOnClick ciickLitener=new TextViewOnClick();
		mTxtAccount.setOnClickListener(ciickLitener);
		mTxtGame.setOnClickListener(ciickLitener);
		mTxtHide.setOnClickListener(ciickLitener);
		mTxtService.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Toast.makeText(mContext,"客服", Toast.LENGTH_SHORT).show();
//				Intent intent=new Intent(mContext,CSServerActivity.class);
//				mContext.startActivity(intent);
			}
		});
		
	}
	
	class TextViewOnClick implements OnClickListener {

		@Override
		public void onClick(View v) {

			Toast.makeText(mContext,""+v.getId(), Toast.LENGTH_SHORT).show();

//			Intent floatMenuOptions = new Intent(mContext, FloatButtonOptionsActivity.class);
//			floatMenuOptions.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//			if(v.getId() == ResourceUtil.getId(mContext, KR.id.txt_floatmenu_account))
//			{
//				floatMenuOptions.putExtra("float_menu_index", ACCOUNT_MANAGER);
//			}
//			else if(v.getId() == ResourceUtil.getId(mContext, KR.id.txt_floatmenu_game))
//			{
//				floatMenuOptions.putExtra("float_menu_index", MORE_GAME);
//			}
//			else if(v.getId() == ResourceUtil.getId(mContext, KR.id.txt_floatmenu_hide))
//			{
//				CloseFloatMenuDialog dialog = new CloseFloatMenuDialog(mContext);
//				dialog.show();
//			}
//
//			if(v.getId() != ResourceUtil.getId(mContext, KR.id.txt_floatmenu_hide))
//			{
//				mContext.startActivity(floatMenuOptions);
//			}






		}
		
	}
	
	public void hideFloatView(){
		if(mManager!=null&&floatView!=null){
			mHandler.removeCallbacks(run);
//			mManager.removeView(floatView);
			mManager.removeViewImmediate(floatView);
			mManager=null;
			existView=false;
		}
//		if(timerhandler!=null){
//			timerhandler.removeMessages(1);
//			timerhandler.removeMessages(2);
//		}
	}

	private void setTimer(){
		if(timerhandler!=null){
			timerhandler.removeMessages(1);
			timerhandler.removeMessages(2);
		}if(timer==null){
			timer=new Timer();
		}else{
			timer.cancel();
			timer=new Timer();
		}
		timer.schedule(new TimerTask() {

			@Override
			public void run() {
				timerhandler.sendEmptyMessage(1);
			}
		}, 5000);
	}

	private Handler timerhandler=new Handler(){
		public void handleMessage(Message msg) {
			if(msg.what==1){

				if(leftLayout!=null&&leftLayout.getVisibility()== View.GONE&&ll_csfloat_hint!=null&&ll_csfloat_hint.getVisibility()== View.GONE){

					ll_csfloat_hint.setVisibility(View.VISIBLE);
					timerhandler.sendEmptyMessageDelayed(2, 3000);
					isRegister=false;
				}

			}else if(msg.what==2){
				ll_csfloat_hint.setVisibility(View.GONE);

			}

		};
	};
	
	
	
	
	
}
